package com.cqjtu.education.service.impl;

import com.cqjtu.education.entity.Course;
import com.cqjtu.education.entity.StudentCourse;
import com.cqjtu.education.dao.StudentCourseDao;
import com.cqjtu.education.dao.CourseDao;
import com.cqjtu.education.dao.UserDao;
import com.cqjtu.education.entity.User;
import com.cqjtu.education.service.StudentCourseService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.*;

/**
 * (StudentCourse)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:49:43
 */
@Service("studentCourseService")
public class StudentCourseServiceImpl implements StudentCourseService {
    @Resource
    private StudentCourseDao studentCourseDao;
    @Resource
    private CourseDao courseDao;
    @Resource
    private UserDao userDao;

    @Override
    public Map<String, String> addByTeacher(String cid, List<String> telList) {
        Map<String, String> map = new HashMap<>();
        for (String tel : telList) {
            User user = userDao.queryByTel(tel);
            //先判断用户是否存在且之前是否加入该课程没有
            if (user != null && studentCourseDao.queryByCidAndSid(cid, user.getId()) == null) {
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setStudentId(user.getId());
                studentCourse.setCourseId(cid);
                studentCourse.setId(UUID.randomUUID().toString());
                if (studentCourseDao.insert(studentCourse) > 0) {
                    map.put(tel, "插入成功");
                } else {
                    map.put(tel, "插入失败，系统错误");
                }
            } else {
                map.put(tel, "插入失败，用户不存在或已加入该课程");
            }
        }
        return map;
    }

    @Override
    public boolean deleteRecord(String cid, String sid) {
        return this.studentCourseDao.deleteRecord(cid, sid) > 0;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StudentCourse queryById(String id) {
        return this.studentCourseDao.queryById(id);
    }

    @Override
    public StudentCourse queryByCidAndSid(String cid, String sid) {
        return this.studentCourseDao.queryByCidAndSid(cid, sid);
    }

    @Override
    public List<Course> queryByStudentId(String id) {
        List<StudentCourse> list = this.studentCourseDao.queryByStudentId(id);
        List<Course> courses = new ArrayList<>();
        for (StudentCourse s : list) {
            Course course = this.courseDao.queryById(s.getCourseId());
            courses.add(course);
        }
        return courses;
    }

    @Override
    public List<User> queryByCourseId(String id) {
        List<StudentCourse> list = this.studentCourseDao.queryByCourseId(id);
        List<User> users = new ArrayList<>();
        for (StudentCourse s : list) {
            User user = this.userDao.queryById(s.getStudentId());
            users.add(user);
        }
        return users;
    }


    /**
     * 新增数据
     *
     * @param studentCourse 实例对象
     * @return 实例对象
     */
    @Override
    public StudentCourse insert(StudentCourse studentCourse) {
        //先判断之前是否加入该课程没有
        if (studentCourseDao.queryByCidAndSid(studentCourse.getCourseId(), studentCourse.getStudentId()) == null) {
            studentCourse.setId(UUID.randomUUID().toString());
            this.studentCourseDao.insert(studentCourse);
            return studentCourse;
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param studentCourse 实例对象
     * @return 实例对象
     */
    @Override
    public StudentCourse update(StudentCourse studentCourse) {
        this.studentCourseDao.update(studentCourse);
        return this.queryById(studentCourse.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.studentCourseDao.deleteById(id) > 0;
    }
}
